package com.lkk.cg.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by lkk on 2014/12/14.
 */
public class FileUtils {
    /**
     * 获取指定文件的字节数组，如果文件不存在，则返回null
     *
     * @param file 需要转换的文件
     * @return
     */
    public static byte[] getBytesFromFile(File file) {
        try {
            InputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            inputStream.close();
            return bytes;
        } catch (Exception e) {
            System.out.println("Fail to read byte array from file...");
            e.printStackTrace();
            return null;
        }

    }

    public static String getFileNameExtension(String fileName) {
        int indexOfDot = fileName.lastIndexOf('.');
        if (indexOfDot == -1)
            return null;
        return fileName.substring(indexOfDot + 1);
    }

    public static String getFileExtension(File file) {
        String fileName = file.getName();
        return FileUtils.getFileNameExtension(fileName);
    }

}
